/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import fr.istia.gesstages.dao.IDaoApply;
import fr.istia.gesstages.dao.IDaoFile;
import fr.istia.gesstages.dao.IDaoOffer;
import fr.istia.gesstages.dao.IDaoUser;
import fr.istia.gesstages.dao.IDaoUserType;
import java.util.List;
import fr.istia.gesstages.jpa.Apply;
import fr.istia.gesstages.jpa.Offer;
import fr.istia.gesstages.jpa.User;
import fr.istia.gesstages.jpa.UserType;
import fr.istia.gesstages.util.Encryption;
import fr.istia.gesstages.util.Access;
import java.util.Date;
import junit.framework.Assert;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author usrlocal
 */
public class InitDB {
    
    private static IDaoApply daoApply;
    private static IDaoUser daoUser;
    private static IDaoUserType daoUserType;
    private static IDaoOffer daoOffer;
    private static IDaoFile daoFile;
    private static Access access;
    
    public InitDB() {
    }
    
    @BeforeClass
    public static void setUpClass() throws Exception {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");
        daoApply = (IDaoApply) ctx.getBean("beanDaoApply");
        daoUser = (IDaoUser) ctx.getBean("beanDaoUser");
        daoUserType = (IDaoUserType) ctx.getBean("beanDaoUserType");
        daoOffer = (IDaoOffer) ctx.getBean("beanDaoOffer");
        daoFile = (IDaoFile) ctx.getBean("beanDaoFile");
        access = (Access) ctx.getBean("beanAccess");
        daoApply.deleteAll();
        daoOffer.deleteAll();
        daoUser.deleteAll();
        daoUserType.deleteAll();
    }
    
    @AfterClass
    public static void tearDownClass() throws Exception {
    }
    
    @Before
    public void setUp() {
    }
    
    @After
    public void tearDown() {
    }
    
    @Test
    public void createEntities() {
        // Types d'utilisateurs
        UserType admin = daoUserType.create(new UserType("Administrateur"));
        UserType etudiant = daoUserType.create(new UserType("Etudiant"));
        UserType invite = daoUserType.create(new UserType("Invité"));
        Assert.assertNotNull(admin);
        Assert.assertNotNull(etudiant);
        Assert.assertNotNull(invite);
        List<UserType> types = daoUserType.findAll();
        Assert.assertEquals(3, types.size());

        // Utilisateurs
        Assert.assertNotNull(daoUser.create(new User(admin, "admin", "21232f297a57a5a743894a0e4a801fc3", "Administrateur", "", "admin@portail-stages.com", false, "")));
        Assert.assertNotNull(daoUser.create(new User(etudiant, "cedric.moreau", "5f4dcc3b5aa765d61d8327deb882cf99", "Moreau", "Cédric", "cem.moreau@gmail.com", true, "EI5")));
        Assert.assertNotNull(daoUser.create(new User(etudiant, "gaetan.loussier", "5f4dcc3b5aa765d61d8327deb882cf99", "LOUSSIER", "Gaëtan", "gaetan.loussier@gmail.com", true, "EI5")));
        Assert.assertNotNull(daoUser.create(new User(etudiant, "hubert.mong", "5f4dcc3b5aa765d61d8327deb882cf99", "Mong", "Hubert", "hubert2000m@gmail.com", false, "EI5")));
        List<User> users = daoUser.findAll();
        User gaetan = daoUser.find("gaetan.loussier");
        User cedric = daoUser.find("cedric.moreau");
        Assert.assertEquals(4, users.size());
        Assert.assertEquals("cedric.moreau", cedric.getLogin());
        Assert.assertEquals("5f4dcc3b5aa765d61d8327deb882cf99", gaetan.getMdp());
        Assert.assertEquals("Gaëtan", gaetan.getPrenom());

        // Offres
        Assert.assertNotNull(daoOffer.create(new Offer(
                "Mission : vous serez également amené(e) à étudier, puis à actualiser le panorama des principales offres technologiques du marché (Oracle BRM, Ericsson/BSCS, SAP/Highdeal…) en appréciant leur capacité à adresser les enjeux de cette vision prospective",
                "Stage en développement Java EE",
                "Sopra Group",
                "Direction du Recrutement\r\n9 bis rue de Presbourg",
                "75116",
                "Paris",
                new Date("10/11/2011"),
                new Date("16/11/2011"),
                true, true, "Test", "", "EI5", "Laurent", "Amot", "Sébastien Délai", false)));
        Assert.assertNotNull(daoOffer.create(new Offer(
                "Etudier et concevoir un système de gestion des offres de stage de l'entreprise",
                "Stage en développement PHP",
                "Capgemini",
                "11 rue de Tilsitt, Place de l'étoile",
                "75017",
                "Paris",
                new Date("20/09/2011"),
                new Date("22/10/2011"),
                true, true, "Test 2", "", "EI5", "Patrick", "Juckolat", "Didier Renaud", true)));
        Assert.assertEquals(2, daoOffer.findAll().size());
    }
    
    @Test
    public void kernelTesting() {
        Assert.assertTrue(access.login("cedric.moreau", Encryption.md5("password")));
        Assert.assertFalse(access.login("cedric.moreau", "password"));
        List<Offer> offers = daoOffer.findCurrent();
        Assert.assertEquals(1, offers.size());
        
        // Offres
        offers = daoOffer.findArchived("2011");
        Assert.assertEquals(1, offers.size());
        // Archivage
        daoOffer.archiveAll();
        Assert.assertEquals(2, daoOffer.findArchived("2011").size());
        Assert.assertEquals(0, daoOffer.findCurrent().size());
        daoOffer.delete(offers.get(0));
        Assert.assertEquals(1, daoOffer.findArchived("2011").size());
        Assert.assertEquals(0, daoOffer.findCurrent().size());
        Offer o = daoOffer.findArchived("2011").get(0);
        o.setArchive(false);
        daoOffer.update(o);
        Assert.assertEquals(0, daoOffer.findArchived("2011").size());
        Assert.assertEquals(1, daoOffer.findCurrent().size());
        
        User user = daoUser.find("cedric.moreau");
        Offer offer = daoOffer.findCurrent().get(0);
        daoApply.create(new Apply(offer, user, 0, new Date()));
        List<Apply> applies = daoApply.findBy(offer);
        Assert.assertEquals(1, applies.size());
    }
}
